
package buildings.net.client;

import buildings.*;
import buildings.Space;
import buildings.dwelling.DwellingFloor;
import buildings.dwelling.Flat;
import buildings.dwelling.hotel.HotelFloor;
import buildings.office.Office;
import buildings.office.OfficeFloor;
import buildings.Buildings;
import buildings.dwelling.DwellingFactory;
import buildings.dwelling.hotel.HotelFactory;
import java.io.*;
import java.net.Socket;

public class BinaryClient {
    public static void main(String[] args) {
        FileReader buildings = null;    //информация о зданиях
        BufferedReader types = null;        //типы зданий
        PrintWriter writer = null;
        PrintWriter out = null;
        BufferedReader in = null;
        Socket clientSocket = null;       
        try {
            buildings = new FileReader("buildings.txt");
            types = new BufferedReader(new FileReader("types.txt"));
            writer = new PrintWriter("cost.txt");
            clientSocket = new Socket("localhost", 6667);
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                    clientSocket.getInputStream()));
            Building buf = null;
            String str = null;  
            
            while((buf = Buildings.readBuilding(buildings)) != null) {
                //считать тип здания
                str = types.readLine();
                out.println(str);
                
                //отправляем информацию о здании
                Buildings.writeBuilding(buf, out);
                
                //получаем стоимость здания
                str = in.readLine();
                //записываем в файл
                writer.println(str);
            }
            //сообщаем серверу о том, что передача закончилась
            out.println("end");
            buildings.close();
            types.close();
            writer.close();
            in.close();
            out.close();
            clientSocket.close();
            System.out.println("Операция успешно завершена");
            
        } catch(IOException e) {
            System.out.println("Операция не удалась");
        }
    }
}
